﻿using System.Collections.Generic;
using UnityEngine;

/**
 * 逻辑实体基类
 */

public class EntityBase{
    public AniState currentAnimState = AniState.None;
    public StateMgr stateMgr = null;
    protected Controller controller = null;
    public SkillMgr skillMgr = null;
    public BattleMgr battleMgr = null;
    
    private BattleProps props;
    public  BattleProps Props{
        get{
            return props;
        }
        protected set{
            props = value;
        }
    }

    private string name;
    public string Name{ get; set; }

    private int hp;

    public int Hp{
        get{
            return hp;
        }
        set{
            //PECommon.Log("hp change:" + hp + " to " + value);
            SetHpVal(hp, value);
            hp = value;
        }
    }

    public virtual void SetBattleProps(BattleProps props){
        Hp = props.hp;
        Props = props;
    }
    
    public bool canControl = true;

    public Queue<int> comboQue = new Queue<int>();
    public int nextSkillID = 0;

    public void Born(){
        stateMgr.ChangeState(this, AniState.Born, null);
    }
    
    public void Move(){
        stateMgr.ChangeState(this, AniState.Move, null);
    }

    public void Idle(){
        stateMgr.ChangeState(this, AniState.Idle, null);
    }
    
    public void Attack(int skillId){
        stateMgr.ChangeState(this, AniState.Attack, skillId);
    }

    public void Die(){
        stateMgr.ChangeState(this, AniState.Die);
    }

    public void Hit(){
        stateMgr.ChangeState(this, AniState.Hit);
    }
    
    public virtual void SetBlend(float blend){
        if (controller != null){
            controller.SetBlend(blend);
        }
    }
    
    public virtual void SetDir(Vector2 dir){
        if (controller != null){
            controller.Dir = dir;
        }
    }

    public virtual void SetAction(int act){
        if (controller != null){
            controller.SetAction(act);
        }
    }

    public virtual void SetFx(string name, float destroy){
        if (controller != null){
            controller.SetFx(name, destroy);
        }
    }
    
    public virtual void SetSkillMoveState(bool move, float speed = 0f){
        if (controller != null){
            controller.SetSkillMoveState(move, speed);
        }
    }
    
    public virtual void SkillAttack(int skillId){
        skillMgr.SkillAttack(this, skillId);
    }
    
    public virtual Vector2 GetDirInput(){
        return Vector2.zero;
    }

    public virtual Vector3 GetPos(){
        return controller.transform.position;
    }

    public virtual Transform GetTransform(){
        return controller.transform;
    }

    public virtual void SetDodge(){
        if (controller != null){
            GameRoot.Instance.dynamicWnd.SetDodge(Name);
        }
    }

    public virtual void SetHurt(int hurt){
        if (controller != null){
            GameRoot.Instance.dynamicWnd.SetHurt(Name, hurt);
        }
    }

    public virtual void SetCritical(int critical){
        if (controller != null){
            GameRoot.Instance.dynamicWnd.SetCritical(Name, critical);
        }
    }

    public virtual void SetHpVal(int oldVal, int newVal){
        if (controller != null){
            GameRoot.Instance.dynamicWnd.SetHpVal(Name, oldVal, newVal);
        }
    }

    public AnimationClip[] GetAniClips(){
        if (controller != null){
            return controller.anim.runtimeAnimatorController.animationClips;
        }

        return null;
    }

    public void SetCtl(Controller ctl){
        controller = ctl;
    }

    public void SetActive(bool isActive = true){
        if (controller != null){
            controller.gameObject.SetActive(isActive);
        }
    }

    public void ExitCurSkill(){
        //PECommon.Log("Exit StateAttack");
        canControl = true;
        if (comboQue.Count > 0){
            nextSkillID = comboQue.Dequeue();
        }
        else{
            nextSkillID = 0;
        }
        SetAction(Constants.ActionDefault);
    }
}
